<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入订单名称">
            <el-button
              slot="append"
              type="info"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 渲染表格视图 -->
      <el-table
        :data="orderList"
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="订单编号"
          prop="order_number"
          width="240px"
        ></el-table-column>
        <el-table-column
          label="商品价格"
          prop="order_price"
        ></el-table-column>
        <el-table-column label="是否支付">
          <template slot-scope="scope">
            <el-tag
              v-if="scope.row.pay_status === '1'"
              type="success"
            >已付款</el-tag>
            <el-tag
              v-else
              type="danger"
            >未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否发货"
          prop="is_send"
        ></el-table-column>
        <el-table-column
          label="创建时间"
          width="180px"
        >
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="140px"
        >
          <template>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showAddress"
            ></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              @click="showProgress"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址的会话面板 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialog"
      width="50%"
    >

      <!-- 显示表单的内容 -->
      <el-form
        :data="addressForm"
        :rules="addressRule"
        ref="addressRef"
      >
        <el-form-item label="选择地址">
          <!-- 显示级联选择器 -->
          <el-cascader
            v-model="addressForm.address1"
            :options="CityData"
            :props="cascaderProps"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input></el-input>
        </el-form-item>
      </el-form>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addressDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addressDialog = false"
        >确 定</el-button>
      </span>
    </el-dialog>

    <!-- 显示物流的会话面板 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialog"
      width="50%"
    >

      <!-- 显示物流的进度 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressList"
          :key="index"
          :timestamp="activity.time"
        >
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="progressDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="progressDialog = false"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import CityData from './citydata.js'

export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderList: [],
      // 修改地址相关参数
      addressForm: {
        address1: [],
        address2: ''
      },
      addressRule: {},
      addressDialog: false,
      CityData,
      cascaderProps: {
        expandTrigger: 'hover'
      },
      progressDialog: false,
      progressList: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      let { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error('数据请求失败')
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange(curPage) {
      this.queryInfo.pagenum = curPage
      this.getOrderList()
    },
    showAddress() {
      this.addressDialog = true
    },
    async showProgress() {
      this.progressDialog = true
      let { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error('物流信息请求失败')
      this.progressList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin-top: 40px;
}

.el-cascader {
  width: 100%;
}
</style>